package com.library.dao;
import java.util.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;


import com.library.entity.Seat;


import my.db.Page;
import my.db.QueryHelper;

public class SeatDao {

	
	//查询得到座位总数
	public int  Count(long id) {
		String sql="select count(*) from ?";
		return (int) QueryHelper.stat(sql, "seat");
	}
	//查询得到每个校区的总数
	public int  CampusCount(String campus) {
		String sql = "select count(*) from seat where campus=?";
		return (int)QueryHelper.stat(sql, campus);
	}
	
	//根据教室号查询座位信息
//	public List<Seat> getSeats(int  classroom) {
//		String sql = "select * from seat where classroom=?";
//		return (List<Seat>)QueryHelper.query(Seat.class, sql, classroom);
//	}
	public List<Seat> getSeats(String name, Object value) {
		String sql = "select * from seat where " + name + "=?";
		return QueryHelper.query(Seat.class, sql, value);
	}
	
	
	//保存
	public void save(Seat s) {
		String sql = "insert into seat values(null,?,?,?,?,null,null,null,null)";
		QueryHelper.update(sql, s.getCampus(), s.getFloor(),s.getClassroom(),s.getNum());
	}
	
	// 管理员修改
	public void update(Seat s) {
		String sql = "update seat set campus=?,floor=?,classroom=?,num=? where id=?";
		QueryHelper.update(sql, s.getCampus(),s.getFloor(), s.getClassroom(),s.getNum(),s.getId());
	}
	//用户修改
	public void update1(Seat s) {
		String sql = "update seat set status=?,name=?,ctime=? where id=?";
		QueryHelper.update(sql, s.getStatus(), s.getName(),s.getCtime(),s.getId());
	}
	
	// 删除
	public void delete(long id) {
		String sql = "delete from seat where id=?";
		QueryHelper.update(sql, id);
	}
	
	/**
	 * 根据ID查询
	 * */
	public Seat get(long id) {
		String sql = "select * from seat where id=?";
		return QueryHelper.queryUnique(Seat.class, sql, id);
	}
	
	/**
	 * 座位分页查询
	 * */
	public Page<Seat> findPage(Page<Seat> page, String campus) {
		List<Object> params = new ArrayList<>();
		String sql = "select * from seat";
		if(StringUtils.isNotBlank(campus)) {
			sql += " where campus like ?";
			params.add("%"+campus+"%");
		}
		if(StringUtils.isNotBlank(page.getField())) {
			sql += String.format(" order by %s %s", page.getField(), page.getType());
		}
		
		List<Seat> data = QueryHelper.queryPage(Seat.class, sql, page.getPage(), page.getLimit(), params.toArray());
		page.setData(data);
		
		String sql2 = sql.replaceFirst("select .*? from", "select count(*) from");
		Long cnt = QueryHelper.stat(sql2, params.toArray());
		page.setCount(cnt);			
		
		return page;
	}
	/*
	 * 个人预约信息	
	 * */
	public Page<Seat> findPagePerson(Page<Seat> page, String name) {
		List<Object> params = new ArrayList<>();
		String sql = "select * from seat";
		if(StringUtils.isNotBlank(name)) {
			sql += " where name like ?";
			params.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(page.getField())) {
			sql += String.format(" order by %s %s", page.getField(), page.getType());
		}
		
		List<Seat> data = QueryHelper.queryPage(Seat.class, sql, page.getPage(), page.getLimit(), params.toArray());
		page.setData(data);
		
		String sql2 = sql.replaceFirst("select .*? from", "select count(*) from");
		Long cnt = QueryHelper.stat(sql2, params.toArray());
		page.setCount(cnt);			
		
		return page;
	}
	public static void main(String args[]) {
		
		SeatDao s=new SeatDao();
		List<Seat> seat=s.getSeats("classroom", 101);
		System.out.println(seat);
		System.out.println(seat.get(0).getNum()+seat.get(0).getCampus()+seat.get(0).getId());
		System.out.println(123);
	}
}
